package com.ecoolex.discount.sales.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountClassify;
import com.ecoolex.discount.sales.entity.DiscountClassify.Column;
import com.ecoolex.discount.sales.enums.DiscountClassifyStatus;
import com.ecoolex.discount.sales.pojo.AddDiscountClassifyDTO;
import com.ecoolex.discount.sales.pojo.EditDiscountClassifyDTO;
import com.ecoolex.discount.sales.pojo.QueryDiscountClassifyDTO;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.google.common.collect.Lists;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;

@GraphQL2Module(type = Type.Admin,description = "折扣分类")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
	key = DiscountClassifyGraphql.AUTH,
	name = "折扣分类管理",
	remark = "折扣分类管理")
public class DiscountClassifyGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-DISCOUNT-CLASSIFY";
	
	@Reference
	private DiscountProductService discountProductService;
	
	@Autowired
	private StorageService storageService;
	
	@TypeField(type = DiscountClassify.class, field = "image", description = "折扣分类图片")
	public StorageInfo coverImage(@Source DiscountClassify source) {
		return storageService.get(source.getImage());
	}
	
	@TypeField(type = DiscountClassify.class, field = "canOnline", description = "能上线")
	public boolean canOnline(@Source DiscountClassify source,AdminMemberAuthority user) {
		return user.auth(AUTH_ONLENE) && DiscountClassifyStatus.canOnline(source.getStatus());
	}
	
	@TypeField(type = DiscountClassify.class, field = "canOffline", description = "能下线")
	public boolean canOffline(@Source DiscountClassify source,AdminMemberAuthority user) {
		return user.auth(AUTH_OFFLENE) && DiscountClassifyStatus.canOffline(source.getStatus());
	}
	
	@TypeField(type = DiscountClassify.class, field = "canEdit", description = "能编辑")
	public boolean canEdit(@Source DiscountClassify source,AdminMemberAuthority user) {
		return user.auth(AUTH_EDIT);
	}
	
	@TypeField(type = DiscountClassify.class, field = "canDel", description = "能删除")
	public boolean canDel(@Source DiscountClassify source,AdminMemberAuthority user) {
		return user.auth(AUTH_DEL) && DiscountClassifyStatus.canOnline(source.getStatus());
	}
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	@QueryField(field = "discountClassify",description = "折扣分类详情")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_GET,name = "折扣分类详情")
	public DiscountClassify get(@NonNull Integer classifyId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return discountProductService.getDiscountClassify(classifyId);
	}
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@QueryField(field = "discountClassifyList",description = "折扣分类列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_QUERY,name = "折扣分类列表")
	public Pagination<DiscountClassify> query(@NonNull QueryDiscountClassifyDTO query,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return discountProductService.queryDiscountClassify(query,sorts, page);
	}
	
	@MutationField(field = "uploadDiscountClassifyImage",description = "上传折扣分类图片")
	public StorageInfo uploadDiscountClassifyImage(@NonNull Upload request, AdminMemberAuthority user) {
		if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
			user.notAuthority();
		}
		UploadSource uploadSource = new UploadSource("DiscountClassify","Image");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(2));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());
        
        return storageService.upload(request,uploadSource,uploadLimit);
	}
	
	public static final String AUTH_ADD = AUTH + "-ADD";
	
	@MutationField(field = "addDiscountClassify", description = "添加折扣分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加折扣分类")
	public DiscountClassify add(@NonNull AddDiscountClassifyDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return discountProductService.addDiscountClassify(record, user.userName());
	}
	
	public static final String AUTH_EDIT = AUTH + "-EDIT";
	
	@MutationField(field = "editDiscountClassify", description = "编辑折扣分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑折扣分类")
	public DiscountClassify edit(@NonNull EditDiscountClassifyDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		DiscountClassify old = discountProductService.getDiscountClassify(record.getClassifyId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "折扣分类不存在");
		BeanUtil.copy(record,
            old,
            Lists.newArrayList(
            		Column.regionType,
            		Column.status,
                    Column.deleted,
                    Column.createUser,
                    Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return discountProductService.editDiscountClassify(old, user.userName());
	}
	
	public static final String AUTH_DEL = AUTH + "-DEL";
	
	@MutationField(field = "delDiscountClassify", description = "删除折扣分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除折扣分类")
	public Boolean delete(@NonNull Integer classifyId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		DiscountClassify record = discountProductService.getDiscountClassify(classifyId);
		Assert.isTrue(Check.notNull(record),ResultCode.VERIFY.build(1), "折扣分类不存在");
		discountProductService.deleteDiscountClassify(record, user.userName());
		return true;
	}
	
	public static final String AUTH_ONLENE = AUTH + "-ONLENE";
	
	@MutationField(field = "onlineDiscountClassify", description = "上线折扣分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLENE, name = "上线折扣分类")
	public Boolean online(@NonNull Integer classifyId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLENE);
		DiscountClassify record = discountProductService.getDiscountClassify(classifyId);
		Assert.isTrue(Check.notNull(record),ResultCode.VERIFY.build(1), "折扣分类不存在");
		discountProductService.onlineDiscountClassify(record, user.userName());
		return true;
	}
	
	public static final String AUTH_OFFLENE = AUTH + "-OFFLENE";
	
	@MutationField(field = "offlineDiscountClassify", description = "下线折扣分类")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLENE, name = "下线折扣分类")
	public Boolean offline(@NonNull Integer classifyId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLENE);
		DiscountClassify record = discountProductService.getDiscountClassify(classifyId);
		Assert.isTrue(Check.notNull(record),ResultCode.VERIFY.build(1), "折扣分类不存在");
		discountProductService.offlineDiscountClassify(record, user.userName());
		return true;
	}
}
